package com.shen.JUC.pool;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Pool2 {
    public static void main(String[] args) {
        //自定义线程池
        /*  AbortPolicy()      ——超出最大线程数量抛出异常
            CallerRunsPolicy() ——哪来的去哪里
            DiscardPolicy()    ——队列满了丢掉任务，不会抛出异常
           DiscardOldestPolicy()--队列满了会和最早的竞争，也不会抛出异常
            */
        ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(2,  //核心线程池大小
                Runtime.getRuntime().availableProcessors(),   //最大线程池容量
                3, TimeUnit.SECONDS,    //超时时间及单位
                new LinkedBlockingQueue<>(3),//候客区
                Executors.defaultThreadFactory(),    //默认工厂
                new ThreadPoolExecutor.DiscardOldestPolicy() //拒绝策略
        );

        try {
            for (int i = 0; i < 10; i++) {
                poolExecutor.execute(()->{
                    System.out.println(Thread.currentThread().getName()+" go");
                });
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            poolExecutor.shutdown();
        }


    }
}
